﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeAxis.Admin.Account
{
    public partial class Regist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            int userId = 0;
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            Interfaces.IUsers iUser = Impls.ImplUsers.GetInstance(dbmanager);
            if (!this.Password.Text.Equals(this.TextBox1.Text))
            {
                this.Page.RegisterClientScriptBlock("login", "<script type=\"text/javascript\">alert('输入的密码不一致，请确认密码')</script>");
            }
            else
            {
                Models.Users user = new Models.Users();
                user.Username = this.UserName.Text;
                var query = iUser.GetSomeObject(user);
                if (query.Count == 0)
                {
                    user.Password = this.Password.Text;
                    user.Enabled = 1;
                    user.Nickname = user.Username;
                    user.Pic = "";
                    userId = iUser.Insert(user);
                }
                else
                {
                    this.Page.RegisterClientScriptBlock("login", "<script type=\"text/javascript\">alert('用户已存在，请重新输入')</script>");
                }
            }
            dbmanager.Commit();
            dbmanager.Dispose();

            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("LoginUserId", userId.ToString(), 100);
            Response.Redirect("/Admin/MyList.aspx");
        }
    }
}